/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.complex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.mapreduce.DB;
import org.sss.mapreduce.DBFactory;
import org.sss.mapreduce.IOConfiguration;
import org.sss.server.DummyException;

public class EndlessDB implements DB, DBFactory {
  @SuppressWarnings("unused")
  private static Logger logger = LoggerFactory.getLogger(EndlessDB.class);
  private static int MIN_KEY_LENGTH = 34;

  boolean readError = false;

  @Override
  public DB getDB(IOConfiguration ioConf, String hostname, int port) {
    return this;
  }

  public synchronized void setError() {
    readError = true;
  }

  @Override
  public synchronized List<byte[]> range(byte[] prefix, byte[] sentinel, int count, boolean binc) throws IOException {
    if (readError) {
      throw new IOException(new DummyException("range"));
    }
    List<byte[]> result = new ArrayList<byte[]>(count*2);

    byte[] content = null;
    if (prefix.length >= MIN_KEY_LENGTH) {
      content = prefix;
    } else {
      content = Arrays.copyOf(prefix, MIN_KEY_LENGTH);
    }

    for (int i = 0;i < count * 2;++i) {
      result.add(content);
    }

    return result;
  }

  @Override
  public synchronized List<byte[]> ranged(byte[] prefix, byte[] sentinel, int count, boolean binc) throws IOException {
    if (readError) {
      throw new IOException(new DummyException("ranged"));
    }
    List<byte[]> result = new ArrayList<byte[]>(count*2);

    byte[] content = null;
    if (prefix.length >= MIN_KEY_LENGTH) {
      content = prefix;
    } else {
      content = Arrays.copyOf(prefix, MIN_KEY_LENGTH);
    }

    for (int i = 0;i < count * 2;++i) {
      result.add(content);
    }

    return result;
  }

  @Override
  public void putlist(byte[][] kvs) throws IOException {
    // Do nothing
  }

  @Override
  public int rangeout(byte[] prefix, byte[] sentinel, int count, boolean binc) throws IOException {
    // Do nothing
    return 0;
  }

  @Override
  public List<byte[]> fwmkeys(byte[] prefix, int max) throws IOException {
    throw new UnsupportedOperationException("");
  }

  @Override
  public byte[] get(byte[] key) throws IOException {
    throw new UnsupportedOperationException("");
  }

  @Override
  public void close() throws IOException {
  }

  @Override
  public List<byte[]> getlist(byte[][] keys) throws IOException {
    throw new UnsupportedOperationException("");
  }

  @Override
  public void outlist(byte[][] keys) throws IOException {
    throw new UnsupportedOperationException("");
  }
}
